#include<bits/stdc++.h>
#define re return
#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long
#define int ll
#define forn(i,a,n) for(int i=a;i<n;i++)
#define sz(s) (int)(s.size())
#define pii pair<int,int>
#define fr first
#define se second
using namespace std;
int n;
const int maxn=2e5+10,maxA=1e6+10;
int a[maxn];
int s[maxn];
int mx_all,mx[maxA];
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        rep(i,n)
        cin>>a[i];
        rep(i,n)
        s[i]=0;
        forn(i,1,n){
            if(a[i]==a[i-1])
            s[i]=a[i];
            s[i]+=s[i-1];
        }
        memset(mx,-0x3f,sizeof(mx));
        mx_all=0;
        forn(i,1,n){
            int dp=max(mx_all+s[i-1],mx[a[i]]+a[i]+s[i-1]);
            mx_all=max(mx_all,dp-s[i]);
            mx[a[i-1]]=max(mx[a[i-1]],dp-s[i]);
        }
        cout<<mx_all+s[n-1]<<"\n";    
    }
    
    re 0;
}